﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WJP.DataPortal.Helper
{
    /// <summary>
    /// Override the DataContract serialization behavior to
    /// use the <see cref="T:System.Runtime.Serialization.NetDataContractSerializer" />.
    /// </summary>
    public class NetDataContractOperationBehavior : DataContractSerializerOperationBehavior
    {
        /// <summary>Create new instance of object.</summary>
        /// <param name="operation">Operation description.</param>
        public NetDataContractOperationBehavior(OperationDescription operation)
          : base(operation)
        {
        }

        /// <summary>Create new instance of object.</summary>
        /// <param name="operation">Operation description.</param>
        /// <param name="dataContractFormatAttribute">Data contract attribute object.</param>
        public NetDataContractOperationBehavior(OperationDescription operation, DataContractFormatAttribute dataContractFormatAttribute)
          : base(operation, dataContractFormatAttribute)
        {
        }

        /// <summary>
        /// Overrided CreateSerializer to return an XmlObjectSerializer which is capable of
        /// preserving the object references.
        /// </summary>
        public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
        {
            return new NetDataContractSerializer(name, ns);
        }

        /// <summary>
        /// Overrided CreateSerializer to return an XmlObjectSerializer which is capable of
        /// preserving the object references.
        /// </summary>
        public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
        {
            return new NetDataContractSerializer(name, ns);
        }
    }
}
